// === RoleDao.java ===
package com.cardgame.dao;

import com.cardgame.model.role.Role;
import com.cardgame.model.role.Skill;
import org.springframework.stereotype.Repository;
import java.io.*;
import java.util.*;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;

@Repository
public class RoleDao implements IRoleDao {
    private static final String ROLE_DATA_PATH = "data/roles.json";
    private final Map<Integer, Role> roleCache = new HashMap<>();
    private final ObjectMapper objectMapper = new ObjectMapper();

    public RoleDao() {
        loadRoles();
    }

    private void loadRoles() {
        try {
            File file = new File(ROLE_DATA_PATH);
            if (!file.exists()) {
                createDefaultRoleData();
            } else {
                List<Role> roles = objectMapper.readValue(file, new TypeReference<>() {
                });
                for (Role role : roles) {
                    roleCache.put(role.getId(), role);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void createDefaultRoleData() {
        try {
            File file = new File(ROLE_DATA_PATH);
            file.getParentFile().mkdirs();

            List<Role> roles = new ArrayList<>();
            int id = 1;
            int warriorLevel = 1;
            for (;warriorLevel <= 10; warriorLevel++) {
                Role warrior = new Role(
                        id,
                        warriorLevel + "级战士",
                        "images/战士立绘.png",
                        "一个强壮的"+ warriorLevel +"级战士",
                        50 * warriorLevel,
                        warriorLevel,
                        new Skill("鼓舞", warriorLevel+"级鼓舞，回复血量", warriorLevel, 0, 0, 0, 0, warriorLevel),
                        100 * warriorLevel);
                id++;
                roles.add(warrior);
            }

            int mageLevel = 1;
            for (;mageLevel <= 10; mageLevel++) {
                Role mage = new Role(
                        id,
                        mageLevel + "级法师",
                        "images/法师立绘.png",
                        "一个强大的"+ mageLevel +"级法师",
                        40 * mageLevel,
                        warriorLevel * 2,
                        new Skill("治疗术", mageLevel+"级治疗术，回复血量", 2*mageLevel, 0, 0, 0, 0, 2*mageLevel),
                        100 * mageLevel);
                id++;
                roles.add(mage);
            }

            int archerLevel = 1;
            for (;archerLevel <= 10; archerLevel++) {
                Role archer = new Role(
                        id,
                        archerLevel + "级弓箭手",
                        "images/弓箭手立绘.png",
                        "一个擅长射击的"+ archerLevel +"级弓箭手",
                        10 * archerLevel,
                        warriorLevel,
                        new Skill("射击", archerLevel+"级射击，造成伤害", 0, 0, 0, -archerLevel, 0, archerLevel),
                        100 * archerLevel);
                id++;
                roles.add(archer);
            }

            for (Role role : roles) {
                roleCache.put(role.getId(), role);
            }

            objectMapper.writeValue(file, roles);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Role getRoleById(Integer id) {
        return roleCache.get(id);
    }

    public List<Role> getAllRoles() {
        return new ArrayList<>(roleCache.values());
    }
}
